home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- #
- # TGraphic.h
- #
- #
- #
- # The TGraphic class encapsulates custom blitting of a graphic to an offscreen GWorld. The
- # current blitters are based on the Encoded Sprite from "Tips of the Mac Gaming Gurus", with
- # a number of additions and refinements. For example, this class handles hit testing for a
- # single point, hit testing any two graphics against each other, and using the sprite data
- # as a mask for copying from a "background" GWorld into the "foreground" GWorld. This class
- # also handles some alignment issues for the 604, which can slow down the normal TGraphic
- # class.
- #
- # Assumptions: Currently, this class assumes that the color table used to encode the sprites
- # is loaded and being used for both GWorlds. The GWorlds must have a bit depth of 8 -- no
- # other bit depth will work properly.
- #
- # All TGraphics share the same destinations, which are set up with the "PrepareDrawing" and
- # "EndDrawing" commands.
- #
- # Note that this class isn't currently intended to be subclassed, so all of the methods are
- # non-virtual. This might change if at some point we determine we need to create a subclass.
- #
- # Author: Timothy Carroll
- # Apple Developer Technical Support
- # timc@apple.com
- #
- # Modification History:
- #
- # 6/1/96 TMC Initial Release
- #
- # Copyright © 1996 Apple Computer, Inc., All Rights Reserved
- #
- #
- # You may incorporate this sample code into your applications without
- # restriction, though the sample code has been provided "AS IS" and the
- # responsibility for its operation is 100% yours. However, what you are
- # not permitted to do is to redistribute the source as "DSC Sample Code"
- # after having made changes. If you're going to re-distribute the source,
- # we require that you make it clear in the source that the code was
- # descended from Apple Sample Code, but that you've made changes.
- #
- *************************************************************************************/
-
- #ifndef _TGRAPHIC_
- #define _TGRAPHIC_
-
- #pragma once
-
- #include <QDOffscreen.h>
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=power
- #endif
-
- enum eDrawFlag
- {
- kDrawGraphic = 0,
- kDrawBackground = 1
- };
-
- class TGraphic
- {
- public:
- /****************************************************************************************************
- Static Creator and Reference Counting
-
- These are the routines that handle the actual creation of the objects, along with reference
- counting and so on. We don't provide macros here, like we did with the TGraphicCollection,
- as these routines are mostly called from that other class and aren't really necessary.
-
- A TGraphic is currently defined by a resource of type 'Gfic'.
- ****************************************************************************************************/
-
- static TGraphic *NewGraphic (SInt16 resID);
-
- void AddReference (void);
- void DisposeReference (void);
-
-
- /****************************************************************************************************
- Constructor/Destructor
-
- Note that the constructor and destructor should not (in general) be called by users. In fact,
- I might try to make them protected later, once I check to make sure that is possible. All
- creation and destruction is handled by the above routines and if you destroy objects yourself,
- BAD THINGS can happen.
- ****************************************************************************************************/
-
- TGraphic (SInt16 resID);
- ~TGraphic(void);
-
-
- /****************************************************************************************************
- Locking and Unlocking
-
- You are never required to lock a graphic -- the routines run fine either way. We provide these
- functions because the main use of this class is for drawing sprites. In a game, most of the
- graphics will be allocated ahead of time and stick around until the game ends. By locking them
- down high, we ensure they won't need to be moved to make room for other handles that might need
- to be allocated.
- ****************************************************************************************************/
- OSErr LockGraphic (void);
- OSErr UnlockGraphic (void);
-
-
- /****************************************************************************************************
- Creating and destroying the graphic
-
- The actual work to create and destroy the collection is done in the following two routines.
- CreateGraphic loads the resource, gets the data and creates any TGraphic objects necessary
- for the collection. DestroyGraphic simply reverses the process.
-
- In addition, a few utility functions are included for loading from different kinds of resources,
- and actually encoding the graphic.
- ****************************************************************************************************/
- OSErr CreateGraphic (void);
- OSErr DestroyGraphic (void);
-
-
- /****************************************************************************************************
- File Output
-
- These routines allow convenient writing of a graphic out to a resource fork, either in the
- compressed format or as a PICT resource.
- ****************************************************************************************************/
-
- OSErr WriteToGraphicResource (void);
- OSErr WriteToPICTResource (void);
- /****************************************************************************************************
- Accessor Functions
-
- We define a few accessor functions to allow clients to get some information. At this point, we
- are advertising the TGraphics as something that can be obtained. We do not immediatly force
- a link to the TGraphic object when we send it to you. If you want a persistant link to the
- TGraphic object, you must create a link to it and properly dispose of it. Otherwise, make sure
- you don't destroy the TGraphic until you are done with the TGraphic.
- ****************************************************************************************************/
-
- // We'll inline the these for speed.
- SInt16 GetResID(void) {return fResID;}
- Rect GetBounds (void) {return fBounds;}
-
- /****************************************************************************************************
- Utility Functions
-
- The meat and potatoes of the TGraphic class -- method calls to draw graphics and
- perform hit testing.
-
- CopyImage accept a point in the local coordinate system of the destination PixMap. If the
- "useBackground" flag is set and a background PixMap was specified, it will draw from the
- background to the destination, using the graphic's data only as a mask. This allows a cheap
- and easy "erase" function.
-
- HitTest accepts a point in coordinates local to the TGraphic's bounds rect, and returns true
- if that point hits a "drawn" portion of the graphic.
-
- Intersect is a static function that accepts two TGraphic objects and the top left coordinate
- for each object. Both points should be in the same coordinate system.
- It returns true if the actual objects intersect each other.
- ****************************************************************************************************/
-
- void CopyImage (SInt32 top, SInt32 left, Boolean useBackground);
- Boolean HitTest (SInt32 v, SInt32 h);
- static Boolean Intersect (TGraphic *object1, TGraphic *object2, SInt32 v1, SInt32 h1, SInt32 v2, SInt32 h2);
-
-
- protected:
-
-
- /****************************************************************************************************
- Protected and Internal Functions
-
- We have four internal functions that do all of the image drawing within the TGraphic object.
- Basically, CopyImage determine whether or not we are clipped or not, and calls the appropriate
- utility function to do the work.
- ****************************************************************************************************/
-
- OSErr LoadFromGraphicResource (void);
- OSErr LoadFromPICTResource (void);
- OSErr LoadFromICN8Resource (void);
- OSErr LoadFromCIconResource (void);
-
- OSErr EncodeGraphic (PixMapHandle theGraphic, Rect *encodeRect);
-
- void GraphicClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
- void GraphicUnclipped (SInt32 top, SInt32 left);
- void BackgroundClipped (SInt32 top, SInt32 left, UInt32 clipLeft, UInt32 clipRight, UInt32 clipTop, UInt32 clipBottom);
- void BackgroundUnclipped (SInt32 top, SInt32 left);
-
- // These will probably never be used in a game, but are useful for dumping them out
- // to a file. For this reason, we only create unclipped version of the routines.
- void DrawMaskUnclipped (SInt32 top, SInt32 left);
- void HitMaskUnclipped (SInt32 top, SInt32 left);
-
-
- /****************************************************************************************************
- Data Structures
-
- The actual data used to hold the TGraphic's data. We make sure that the data is aligned
- properly for both PowerPC and 68K struct alignment.
-
- fBounds is a zero coordinate based rectangle that holds the actual size of the sprite. We
- get this from the sprite data when we load it and use it in all other routines.
- ****************************************************************************************************/
-
-
- Rect fBounds; // 8 bytes
-
- // This handle holds the data the TGraphic class uses to draw itself
- Handle fImage; // 4 bytes
- // This handle holds the data the TGraphic class uses for hit testing
-
- Handle fHitMask; // 4 bytes
-
- SInt16 fResID; // 2 bytes
-
- UInt16 fReferenceCount; // 2 bytes
-
- UInt16 fBitDepth; // 2 bytes
-
- UInt16 fFlags; // 2 bytes
-
- };
-
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=reset
- #endif
-
- #endif /* _TGRAPHIC_ */